#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 55;

namespace F { // max_flow
    const int N = 55 * 6;
    const int M = N * N;
    const int inf = 0x3f3f3f3f;
    int h[N],now[N],d[N],cnt = 1,S,T;
    struct edge { int v,w,nxt; } e[M << 1];
    inline void tlink(int x,int y,int w) { e[++cnt] = (edge) { y,w,h[x] }; h[x] = cnt; }
    inline void link(int x,int y,int w) { tlink(x,y,w); tlink(y,x,0); }
    inline void setst(int s,int t) { S = s; T = t; }
    inline void reset() { cnt = 1; memset(h,0,sizeof h); memset(now,0,sizeof now); }
    queue < int > q;
    bool bfs() {
	while(q.size()) q.pop(); q.push(S); memset(d,0,sizeof d); d[S] = 1; now[S] = h[S];
	while(!q.empty()) {
	    int x = q.front(); q.pop();
	    for(int i = h[x],y;i;i = e[i].nxt)
		if(e[i].w && !d[y = e[i].v]) {
		    d[y] = d[x] + 1; q.push(y); now[y] = h[y];
		    if(y == T) return 1;
		}
	} return 0;
    }
    int dinic(int x,int flow) {
	if(x == T) return flow;
	int res = flow;
	for(int i = now[x],y;i && res;i = e[i].nxt) {
	    now[x] = i;
	    if(e[i].w && d[y = e[i].v] == d[x] + 1) {
		int k = dinic(y,min(res,e[i].w));
		if(!k) d[y] = 0; e[i].w -= k; e[i ^ 1].w += k; res -= k;
	    }
	}
	return flow - res;
    }
    ll runit() {
	ll maxflow = 0,flow;
	while(bfs()) while(flow = dinic(S,inf)) maxflow += flow;
	return maxflow;
    }
};

int n,m;
char s[N][N];

bool check(int x) {
    F :: reset(); F :: setst(0,n * 6 + 1);
    for(int i = 1;i <= n;i++) {
	F :: link(0,i,x); F :: link(i,i + n,x); F :: link(i,i + n + n,m);
	for(int j = 1;j <= n;j++)
	    if(s[i][j] == 'Y') F :: link(i + n,j + n * 3,1);
	    else F :: link(i + n + n,j + n * 4,1);
    }
    for(int i = 1;i <= n;i++) F :: link(i + n * 3,i + n * 5,x),F :: link(i + n * 4,i + n * 5,m),F :: link(i + n * 5,n * 6 + 1,x);
    return F :: runit() == n * x;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    n = in,m = in;
    for(int i = 1;i <= n;i++) scanf("%s",s[i] + 1);
    int l = 1,r = n;
    while(l <= r) {
	int mid = l + r >> 1;
	if(check(mid)) l = mid + 1;
	else r = mid - 1;
    }
    printf("%d\n",l - 1); return 0;
}
